package com.example.forestmanagement.ui.main;


import static android.content.pm.PackageManager.PERMISSION_GRANTED;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSONArray;
import com.example.forestmanagement.R;
import com.example.forestmanagement.databinding.ActivityMain2Binding;
import com.example.forestmanagement.ui.login.LoginActivity;
import com.example.forestmanagement.ui.map.MapFragment;
import com.example.forestmanagement.ui.user.PersonalFragment;
import com.example.forestmanagement.vm.LoginVM;
import com.example.forestmanagement.vm.MainVM;

import java.util.Map;
import java.util.Objects;

import io.openim.android.ouicontact.ui.fragment.ContactFragment;
import io.openim.android.ouiconversation.ui.fragment.ConversationListFragment;
import io.openim.android.ouicore.base.BaseActivity;
import io.openim.android.ouicore.base.BaseApp;
import io.openim.android.ouicore.base.BaseFragment;
import io.openim.android.ouicore.base.vm.injection.Easy;
import io.openim.android.ouicore.im.IMEvent;
import io.openim.android.ouicore.im.IMUtil;
import io.openim.android.ouicore.net.bage.GsonHel;
import io.openim.android.ouicore.services.CallingService;
import io.openim.android.ouicore.utils.ActivityManager;
import io.openim.android.ouicore.utils.Common;
import io.openim.android.ouicore.utils.Constants;
import io.openim.android.ouicore.utils.Routes;
import io.openim.android.ouicore.vm.NotificationVM;
import io.openim.android.ouicore.vm.UserLogic;
import io.openim.android.sdk.listener.OnAdvanceMsgListener;
import io.openim.android.sdk.models.CustomElem;
import io.openim.android.sdk.models.SignalingInfo;
import io.openim.android.sdk.models.SignalingInvitationInfo;

@Route(path = Routes.Main.HOME)
public class MainActivity extends BaseActivity<MainVM, ActivityMain2Binding> implements LoginVM.ViewAction {

    private int mCurrentTabIndex;
    //    private BaseFragment lastFragment, conversationListFragment, contactFragment,
//        personalFragment, appletFragment;
    private BaseFragment lastFragment, conversationListFragment, contactFragment, personalFragment;
    private MapFragment mapFragment;
    private double lat;
    private double lon;
    private String des;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        bindVM(MainVM.class);
        init();
        vm.fromLogin = getIntent().getBooleanExtra(LoginActivity.FORM_LOGIN, false);
        bindViewDataBinding(ActivityMain2Binding.inflate(getLayoutInflater()));
        super.onCreate(savedInstanceState);
        setLightStatus();

        view.setMainVM(vm);

        click();
        listener();
        view.men1.setChecked(true);
        requestPermissino();
    }

    private void requestPermissino() {
        /**    地图所需权限 :
         * roup:android.permission-group.LOCATION(定位)
         * android.permission.ACCESS_FINE_LOCATION(通过gps获取定位权限)
         * android.permission.ACCESS_COARSE_LOCATION(通过wifi和移动基站获取定位权限)
         **/
        if (ContextCompat.checkSelfPermission(this,
                // 未开启定位权限
                Manifest.permission.ACCESS_FINE_LOCATION) != PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {//用户同意权限,执行我们的操作
            if (grantResults[0] != PERMISSION_GRANTED) {
                //用户拒绝之后,当然我们也可以弹出一个窗口,直接跳转到系统设置页面
                Toast.makeText(this, "未开启定位权限,请手动到设置去开启权限", Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent); // 更新当前页面的 Intent
        lat = getIntent().getDoubleExtra("lat", 0);
        lon = getIntent().getDoubleExtra("lon", 0); // 0 是默认值
        des = getIntent().getStringExtra("des");
        String a = getIntent().getStringExtra("id");
        String b = getIntent().getStringExtra("labelId");
        if (null != des) {
            mapFragment.setMsgLocation(lon,lat,des,a,b);
            switchFragment(mapFragment);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }


    private void init() {
        Easy.find(UserLogic.class).loginCacheUser(userId -> {
            if (!TextUtils.isEmpty(userId))
                vm.initOfflineNotificationConfig(MainActivity.this, userId);
        });
    }

    private void listener() {
        vm.totalUnreadMsgCount.observe(this, v -> Common.buildBadgeView(this, view.men1, v));
        handleCallingMsg();
    }

    private void handleCallingMsg() {
        CallingService callingService = (CallingService) ARouter.getInstance().build(Routes.Service.CALLING).navigation();
        if (callingService != null) {
            IMEvent.getInstance().addAdvanceMsgListener(new OnAdvanceMsgListener() {
                @Override
                public void onRecvOnlineOnlyMessage(String msg) {
                    Map map = JSONArray.parseObject(msg, Map.class);
                    if (map.containsKey("customElem")) {
                        String customElemStr = String.valueOf(map.get("customElem"));
                        if (!"null".equals(customElemStr)) {
                            CustomElem customElem = JSONArray.parseObject(customElemStr, CustomElem.class);
                            if (!TextUtils.isEmpty(customElem.getData())) {
                                Map customMap = JSONArray.parseObject(customElem.getData(), Map.class);
                                if (customMap.containsKey(Constants.K_CUSTOM_TYPE)) {
                                    int customType = Objects.requireNonNullElse((Integer) customMap.get(Constants.K_CUSTOM_TYPE), -1);
                                    String result = String.valueOf(customMap.get(Constants.K_DATA));
                                    if (!"null".equals(result) && customType >= Constants.MsgType.callingInvite
                                            && customType <= Constants.MsgType.callingHungup) {
                                        SignalingInvitationInfo signalingInvitationInfo = GsonHel.fromJson(result, SignalingInvitationInfo.class);
                                        SignalingInfo signalingInfo = new SignalingInfo();
                                        signalingInfo.setInvitation(signalingInvitationInfo);

                                        switch (customType) {
                                            case Constants.MsgType.callingInvite:
                                                callingService.onReceiveNewInvitation(signalingInfo);
                                                break;
                                            case Constants.MsgType.callingAccept:
                                                callingService.onInviteeAccepted(signalingInfo);
                                                break;
                                            case Constants.MsgType.callingReject:
                                                callingService.onInviteeRejected(signalingInfo);
                                                break;
                                            case Constants.MsgType.callingCancel:
                                                callingService.onInvitationCancelled(signalingInfo);
                                                break;
                                            case Constants.MsgType.callingHungup:
                                                callingService.onHangup(signalingInfo);
                                                break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            });
        }
    }


    private void bindDot() {
        NotificationVM notificationVM = Easy.find(NotificationVM.class);
        notificationVM.friendDot.observe(this, v -> view.badge.setVisibility((notificationVM.hasDot()) ?
                View.VISIBLE : View.GONE));
        notificationVM.groupDot.observe(this, v -> view.badge.setVisibility((notificationVM.hasDot()) ?
                View.VISIBLE : View.GONE));
    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RadioButton[] menus = new RadioButton[]{view.men1, view.men2, view.men3, view.men4};
            if (v == view.men1)
                switchFragment(conversationListFragment);
            if (v == view.men2) switchFragment(contactFragment);
            if (v == view.men3) switchFragment(mapFragment);
            if (v == view.men4) switchFragment(personalFragment);
            for (RadioButton menu : menus) {
                menu.setChecked(menu == v);
            }
        }
    };

    private void click() {
        view.men1.setOnClickListener(clickListener);
        view.men2.setOnClickListener(clickListener);
        view.men3.setOnClickListener(clickListener);
        view.men4.setOnClickListener(clickListener);
    }


    @Override
    public void jump() {
        //token过期
        if (vm != null)
            vm.clearOfflineNotificationConfig(ActivityManager.getActivityStack().pop(), BaseApp.inst().loginCertificate.userID);
        IMUtil.logout(this, LoginActivity.class);
    }

    @Override
    public void err(String msg) {

    }

    @Override
    public void succ(Object o) {

    }

    @Override
    public void initDate() {
        contactFragment =
                (ContactFragment) ARouter.getInstance().build(Routes.Contact.HOME).navigation();
        conversationListFragment =
                (ConversationListFragment) ARouter.getInstance().build(Routes.Conversation.CONTACT_LIST).navigation();

        personalFragment = PersonalFragment.newInstance().setParentViewModel(vm);

        mapFragment = new MapFragment();

        personalFragment.setPage(4);
        switchFragment(personalFragment);

        if (null != mapFragment) {
            mapFragment.setPage(3);
            switchFragment(mapFragment);
        } else {
            view.men3.setVisibility(View.GONE);
        }

        if (null != contactFragment) {
            contactFragment.setPage(2);
            switchFragment(contactFragment);
        }
        if (null != conversationListFragment) {
            conversationListFragment.setPage(1);
            switchFragment(conversationListFragment);
        }
        Common.UIHandler.postDelayed(this::bindDot, 500);
    }


    /**
     * 切换Fragment
     */
    private void switchFragment(BaseFragment fragment) {
        try {
            if (fragment != null && !fragment.isVisible() && mCurrentTabIndex != fragment.getPage()) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                if (!fragment.isAdded()) {
                    transaction.add(R.id.fragment_container, fragment);
                }
                if (lastFragment != null) {
                    transaction.hide(lastFragment);
                }
                transaction.show(fragment).commit();
                lastFragment = fragment;
                mCurrentTabIndex = lastFragment.getPage();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
